package com.example.gw_back_end.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.gw_back_end.domain.PageParam;
import com.example.gw_back_end.domain.PageResult;
import com.example.gw_back_end.domain.SearchParams;
import com.example.gw_back_end.domain.document.GameInfoDocument;
import com.example.gw_back_end.domain.entities.Category;
import com.example.gw_back_end.domain.entities.Game;
import com.example.gw_back_end.domain.entities.GameCategory;
import com.example.gw_back_end.mapper.GameCategoryMapper;
import com.example.gw_back_end.mapper.GameMapper;
import com.example.gw_back_end.repository.GameInfoRepository;
import com.example.gw_back_end.service.GameService;
import com.example.gw_back_end.service.mongodb.GameInfoService;
import com.example.gw_back_end.utils.ThreadLocalUtil;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Optional;

@Service
public class GameServiceImpl extends ServiceImpl<GameMapper, Game> implements GameService {

    private final GameMapper gameMapper;

    //    private final GameCategoryMapper gameCategoryMapper;
    private final GameInfoService gameInfoService;
    public GameServiceImpl(GameMapper gameMapper/*, GameCategoryMapper gameCategoryMapper*/, GameInfoService gameInfoService) {
        this.gameMapper = gameMapper;
//        this.gameCategoryMapper = gameCategoryMapper;
        this.gameInfoService = gameInfoService;
    }

    // 分页获取游戏列表
    @Override
    public PageResult<Game> getGameList(PageParam pageParam, List<Integer> categoryIdList,String clientType) {
        if (categoryIdList == null || categoryIdList.isEmpty()) {
            Map<String, Object> claims = ThreadLocalUtil.get();
            Integer accountType = (Integer) claims.get("accountType");
            Integer userId = (Integer) claims.get("id");

            LambdaQueryWrapper<Game> queryWrapper = new LambdaQueryWrapper<>();
            if (accountType.equals(1) || accountType.equals(2)) {
                queryWrapper.eq(Game::getUserId, userId);
            }

            Page<Game> gamePage = gameMapper.selectPage(new Page<>(pageParam.getPage(), pageParam.getSize()), queryWrapper);
            return PageResult.setPageResult(gamePage);
        } else {
            return null;
        }
    }

    // 新增游戏
    @Override
    public boolean addGame(Game game) {
        Map<String, Object> claims = ThreadLocalUtil.get();
        Integer userId = (Integer) claims.get("id");
        Integer accountType = (Integer) claims.get("accountType");

        if (accountType.equals(1) || accountType.equals(2)) {
            game.setUserId(userId);
            game.setStatus(1);

            // 2. 先插入 game，自动生成 id（前提是使用了自增主键，并且 MyBatis 配置好了 useGeneratedKeys）
            int insertResult = gameMapper.insert(game);
            if (insertResult <= 0) {
                return false;
            }

            // 3. 拿到 game.id 并设置给 gameInfo
            GameInfoDocument gameInfo = game.getGameInfo();
            gameInfo.setGameId(String.valueOf(game.getId()));

            // 4. 插入 gameInfo
            gameInfoService.createGameInfo(gameInfo);

            return true;
        } else {
            return false;
        }
    }

    // 删除游戏
    @Override
    public boolean deleteGame(Integer id) {
        return gameMapper.deleteById(id) > 0;
    }

    // 获取游戏
    @Override
    public Game getGameById(Integer id) {
        Game game = gameMapper.selectById(id);
        Optional<GameInfoDocument> optionalGameInfo = gameInfoService.getGameInfoById(String.valueOf(id));
        optionalGameInfo.ifPresent(game::setGameInfo);
        return game;
    }

    // 更新游戏
    @Override
    public boolean updateGame(Game game) {
        System.out.println(game);
        return gameMapper.updateById(game) > 0;
    }

    @Override
    public PageResult<Game> searchGame(SearchParams searchParams) {
        String keyword = searchParams.getKeyword();
        LambdaQueryWrapper<Game> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(Game::getTitle, keyword);
        Page<Game> gamePage = gameMapper.selectPage(new Page<>(searchParams.getPageParam().getPage(), searchParams.getPageParam().getSize()), lambdaQueryWrapper);
        return PageResult.setPageResult(gamePage);
    }
}
